package com.eat.controller.admin;

import com.eat.dto.DishDTO;
import com.eat.dto.DishPageQueryDTO;
import com.eat.entity.Dish;
import com.eat.result.PageResult;
import com.eat.result.Result;
import com.eat.service.DishService;
import com.eat.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加菜品和口味
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){

        dishService.saveWithFlavor(dishDTO);

        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){

       PageResult dishVO =  dishService.page(dto);

        return Result.success(dishVO);
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品删除");
        dishService.deleteBatch(ids);
       return Result.success();
    }

    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){

        DishVO dishVO = dishService.getById(id);

        return Result.success(dishVO);
    }
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){

        dishService.updateWithFlavor(dishDTO);

        return Result.success();
    }
    @GetMapping("list")
    public Result<List<Dish>> getDishById(Long categoryId){

        List<Dish> dishes = dishService.getDishById(categoryId);

        return Result.success(dishes);
    }

    public void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
